package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.CategoryService;
import com.spzx.product.service.ProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName ProductSpecController
 * @Author 波
 * @Date 2024/7/29 20:17
 */
@RestController
@RequestMapping("/productSpec")
@Tag(name = "商品规格接口")
public class ProductSpecController extends BaseController {
    @Resource
    private ProductSpecService productSpecService;
    @Resource
    private CategoryService categoryService;

    @GetMapping("/page")
    @Operation(summary = "获取商品规格的分页信息")
    public TableDataInfo productSpecList(ProductSpec productSpec) {
        startPage();
        List<ProductSpec> productSpecList = productSpecService.productSpecList(productSpec);
        return getDataTable(productSpecList);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取商品规格的详情信息")
    public AjaxResult productSpecList(@PathVariable Long id) {
        ProductSpec productSpec = productSpecService.getById(id);
        productSpec.setCategoryIdList(categoryService.getAllCategoryIdList(productSpec.getCategoryId()));
        return AjaxResult.success(productSpec);

    }

    @PostMapping
    @Operation(summary = "新增商品规格信息")
    public AjaxResult addProductSpec(@RequestBody ProductSpec productSpec) {
        return toAjax(productSpecService.save(productSpec));

    }
    @PutMapping
    @Operation(summary = "修改商品规格信息")
    public AjaxResult updateProductSpec(@RequestBody ProductSpec productSpec) {
        return toAjax(productSpecService.updateById(productSpec));
    }
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除商品规格信息")
    public AjaxResult deleteProductSpec(@PathVariable Long[] ids) {
        return toAjax(productSpecService.removeBatchByIds(Arrays.asList(ids)));
    }
    @GetMapping("/productSpecList/{categoryId}")
    @Operation(summary = "根据分类Id查询商品规格信息列表")
    public AjaxResult selectProductSpecListByCategoryId(@PathVariable Long categoryId) {
        LambdaQueryWrapper<ProductSpec> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSpec::getCategoryId, categoryId);
        List<ProductSpec> productSpecList=productSpecService.list(queryWrapper);
        return AjaxResult.success(productSpecList);
    }

}
